<script setup lang="ts">
import {onBeforeUnmount, ref, watch} from "vue";
import useCategoryStore from '@/store/modules/category.ts'
import {reqHasSPU,reqSkuList,reqRemoveSpu} from '@/api/product/spu/index.ts'
import type {HasSpuResponseData,Records,SpuData,SkuInfoData,SkuData} from '@/api/product/spu/type.ts'
import SpuForm from './spuForm.vue'
import SkuForm from './skuForm.vue'
import {ElMessage} from "element-plus";

const scene= ref<number>(0)
const pageNo=ref<number>(1)
const pageSize=ref<number>(3)
const total = ref<number>(0)
const categoryStore =useCategoryStore()
const records = ref<Records>([])
/**
 * 获取某三级分类下所有SPU
 */
const getHasSpu = async (pager=1) => {
  pageNo.value=pager
  const res:HasSpuResponseData =await reqHasSPU(pageNo.value,pageSize.value,categoryStore.c3Id)
  if(res.code==200){
    records.value=res.data.records
  }
  total.value=res.data.total
  // console.log(res)
}
//监听三级分类ID变化
watch(()=>categoryStore.c3Id,()=>{
  if(!categoryStore.c3Id){
    return
  }
  getHasSpu()
},{immediate:true})
/**
 * 改变每页数量时触发
 */
const changeSize = () => {
  getHasSpu()
}
/**
 * 添加spu
 */
const addSpu = () => {
  scene.value=1
  // 添加SPU初始化数据
  spu.value?.initAddSpu(categoryStore.c3Id)
}
/**
 * 子组件取消触发
 * @param val
 */
const changeScene = ({flag,params}:{flag:any,params:any}) => {
  scene.value=flag
  if(params=='update'){
    getHasSpu(pageNo.value)
  }else{
    getHasSpu()
  }
}
const spu = ref()
/**
 * 修改Spu
 */
const updateSpu = (row:SpuData) => {
  // console.log(row)
  spu.value?.initHasSpuData(row)
  scene.value=1
}
//SkuForm组件的引用
const sku= ref<any>()
/**
 * 添加SKU
 */
const addSKu = (row:SpuData) => {
  scene.value=2
  sku.value?.initSkuData(categoryStore.c1Id,categoryStore.c2Id,row)
}
//存储全部的sku数据
const skuArr = ref<SkuData[]>()
// 对话框显示
const show =ref<boolean>(false)
/**
 * 查看全部的sku
 * @param row
 */
const findSku = async (row:any) => {
  const res:SkuInfoData = await reqSkuList(row.id)
  console.log(res)
  if(res.code==200){
    skuArr.value=res.data
    show.value=true
  }
}
/**
 * 删除一个spu
 * @param id
 */
const deleteSpu = async (id:number) => {
  const res= await reqRemoveSpu(id)
  if(res.code==200){
    ElMessage({
      type:'success',
      message:'删除成功'
    })
    await getHasSpu(records?.value.length>1?pageNo.value:pageNo.value-1)
  }else{
    ElMessage({
      type:'error',
      message:'删除失败'
    })
  }
}
//组件销毁清空数据
onBeforeUnmount(()=>{
  categoryStore.$reset()
})
</script>

<template>
  <div>
    <Category :scene="scene"></Category>
    <el-card style="margin-top: 10px;">
      <div v-show="scene==0">
        <el-button type="primary" icon="Plus" :disabled="!categoryStore.c3Id" @click="addSpu">添加SPU</el-button>
        <el-table style="margin: 10px 0;" border :data="records">
          <el-table-column label="序号" type="index" align="center" width="100px"></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column label="SPU描述" prop="description" show-overflow-tooltip></el-table-column>
          <el-table-column label="SPU操作">
            <template #default="{row}">
              <el-button type="primary" size="small" icon="Plus"  @click="addSKu(row)"></el-button>
              <el-button type="warning" size="small" icon="Edit"  @click="updateSpu(row)"></el-button>
              <el-button type="info" size="small" icon="View" @click="findSku(row)"></el-button>
              <el-popconfirm :title="`你确定要删除${row.spuName}吗？`" width="200px" @confirm="deleteSpu(row.id)">
                <template #reference>
                  <el-button type="danger" size="small" icon="Delete"></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
            v-model:current-page="pageNo"
            v-model:page-size="pageSize"
            :page-sizes="[3,5,7,9]"
            :disabled="false"
            :background="true"
            layout="prev, pager, next, jumper,->,sizes,total"
            :total="total"
            style="margin-top: 15px"
            @current-change="getHasSpu"
            @size-change="changeSize"
        />
      </div>
<!--      添加和修改spu-->
      <SpuForm v-show="scene==1" ref="spu" @changeScene="changeScene"/>
<!--      添加sku的子组件-->
      <SkuForm v-show="scene==2" ref="sku"  @changeScene="changeScene"/>
<!--      对话框-->
      <el-dialog title="SKU列表" v-model="show">
        <el-table border :data="skuArr">
          <el-table-column label="SKU名字" prop="skuName"></el-table-column>
          <el-table-column label="SKU价格" prop="price"></el-table-column>
          <el-table-column label="SKU重量" prop="weight"></el-table-column>
          <el-table-column label="SKU图片">
            <template #default="{row}">
              <el-image :src="row.skuDefaultImg" style="width: 100px;height: 100px;">
              </el-image>
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<style scoped lang="scss">

</style>